/*
 * Copyright 2009 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.splink.deepsplink.injector {
	import org.splink.deepsplink.authentication.IAuthenticator;
	import org.splink.deepsplink.interceptor.IInterceptor;
	import org.splink.deepsplink.page.IAuthenticationPage;
	import org.splink.deepsplink.page.IPage;

	/**
	 * Checks whether a page is an <code>IAuthenticationPage</code>. If this is true 
	 * two objects are injeted into the page.
	 * 
	 * Firstly the <code>IAuthenticator</code> which is registered with the 
	 * <code>IDeepsplinkFacade</code>
	 * Secondly an <code>IInterceptor</code> which inspects page requests and redirects 
	 * to the <code>IAuthenticationPage</code> if the user isn't authenticated.
	 * 
	 * @author Max Kugland
	 */
	final public class AuthenticationInjector implements IPageInjector {
		private var _auth : IAuthenticator;
		private var _security : IInterceptor;

		public function AuthenticationInjector(auth : IAuthenticator, security : IInterceptor) {
			_auth = auth;
			_security = security;
		}
		
		/**
		 * @inheritDoc
		 */
		public function injectPage(page : IPage) : void {
			if(page is IAuthenticationPage) {
				var p : IAuthenticationPage = IAuthenticationPage(page);
				p.authenticator = _auth;
				p.securityInterceptor = _security;
			}
		}
	}
}
